package com.example.hasee.login;

import com.example.hasee.data.DataCallback;
import com.example.hasee.data.DataStore;
import com.example.hasee.data.entity.Student;

public class LoginPresenter implements LoginContract.Presenter{
    private LoginContract.View rootView;
    private DataStore mDataStore;
    public LoginPresenter(LoginContract.View rootView){
        this.rootView=rootView;
        mDataStore=DataStore.getINSTANCE();
    }
    @Override
    public void login(String name, String password) {
        //完成登陆功能
        rootView.showLoadingView();
        final Student student=new Student();
        student.setUsername(name);
        student.setPassword(password);
        mDataStore.userLogin(student, new DataCallback<Student>() {
            @Override
            public void onSuccess(Student data) {
                //隐藏加载界面
                rootView.hideLoadingView();
                //成功跳转
                rootView.toHomeView();
            }
            @Override
            public void onFail(String message) {
                rootView.hideLoadingView();
                //显示错误信息
                rootView.showError(message);
            }
        });
    }

    @Override
    public void signUp(String name, String password) {
        //完成注册功能
        rootView.showLoadingView();
        final Student student=new Student();
        student.setUsername(name);
        student.setPassword(password);
        mDataStore.userSignUp(student, new DataCallback<Student>() {
            @Override
            public void onSuccess(Student data) {
                //隐藏加载界面
                rootView.hideLoadingView();
                //成功跳转
                rootView.toHomeView();
            }
            @Override
            public void onFail(String message) {
                rootView.hideLoadingView();
                //显示错误信息
                rootView.showError(message);
            }
        });
    }
}
